package com.example.androidshixun;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidshixun.utils.PreferenceUtils;
import com.google.android.material.snackbar.Snackbar;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userName, pwdTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        TextView registerBtn = findViewById(R.id.register);
        TextView backBtn = findViewById(R.id.login_back);
        userName = findViewById(R.id.user_name_tv);
        pwdTv = findViewById(R.id.pwd_tv);
        Button loginBtn = findViewById(R.id.login_btn);

        registerBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        PreferenceUtils.getInstance(this).saveLoginStatus(false, "");

    }

    private int requestRegister = 9527;
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register:
                Intent intent = new Intent(this, RegisterActivity.class);
                startActivityForResult(intent, requestRegister);
                break;
            case R.id.login_back:
                finish();
                break;
            case R.id.login_btn:
                PreferenceUtils.getInstance(this).saveLoginStatus(false, "");
                String pwd = pwdTv.getText().toString();
                String name = userName.getText().toString();
                if(name.equals("")) {
                    Snackbar.make(view, "请输入用户名", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(pwd.equals("")) {
                    Snackbar.make(view, "请输入密码", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                String realPwd = PreferenceUtils.getInstance(this).getPassword(name);
                if(realPwd.equals(pwd)) {

                    PreferenceUtils.getInstance(this).saveLoginStatus(true, name);
                    Intent data = new Intent();
                    data.putExtra(Const.USER_NAME, name);
                    setResult(RESULT_OK, data);
                    finish();
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == requestRegister) {
            if(data != null) {
                String newName = data.getStringExtra(Const.NEW_USER_NAME);
                userName.setText(newName);
                pwdTv.setText("");
            }
        }
    }
}
